/* app功能 */
(function(owner) {
	/*
	 * 延时执行隐藏窗口方法
	 * 为了解决打开新页面，隐藏当前页面时遇到的还没有完全打开新页面，当前页面就已经关闭的问题
	 */
	owner.timeoutBack = function() {
		setTimeout(function() {
			plus.webview.currentWebview().hide();
		}, 500);
	}
	
	/* 配置node后台服务地址 */
	owner.getUrl = function() {
		//let url = 'http://192.168.1.102:7001'
		let url = 'http://61.182.24.104:7001';
		return url
	}
	/* 配置java后台服务地址 */
	owner.getJavaUrl = function() {
		let url = 'http://61.182.24.104:8089';
		return url
	}
	/* 配置apache服务地址 */
	owner.getApacheUrl = function() {
		let url = 'http://61.182.24.104:8888';
		return url
	}
	/* 
	 * 将信息存储到本地
	 */
	owner.setStorage = function(key, value) {
		localStorage.setItem(key, JSON.stringify(value));
	}
	/* 
	 * 获取本地存储资源
	 */
	owner.getStorage = function(key) {
		let value = window.localStorage.getItem(key) || '{}';
		return JSON.parse(value);
	}
	/* 清除本地数据 */
	owner.delStorage = function(key) {
		if (key) {
			//清除传入key
			window.localStorage.removeItem(key);
		} else {
			//如果不传参数，清除全部
			window.localStorage.clear();
		}
	}
	/* 
	 * app登录状态
	 */
	owner.loginState = function() {
		let state = false;
		if (window.localStorage.hasOwnProperty('customerInfo')) {
			if (app.getStorage('customerInfo').gid) {
				state = true
			}
		} 
		return state;
	}

	/* 显示页面 */
	owner.open = function(page) {
		mui.openWindow({
			url: page + '.html',
			show: {
				aniShow: 'pop-in',
				duration: 300
			}
		});
	}
	/* 
	 * 对ajax进行二次封装
	 * 加入token验证
	 */
	owner.ajax = function(path, data, method) {
		data = data || null;
		method = method || null;
		return new Promise(function(resolve) {
			mui.ajax(owner.getUrl() + path, {
				data: data,
				dataType: 'json', //服务器返回json格式数据
				type: method, //HTTP请求类型
				timeout: 10000, //超时时间设置为10秒；
				beforeSend: function(request) {
					request.setRequestHeader("Authorization", owner.getStorage('customerInfo').token);
				},
				success: function(data) {
					//如果返回错误码为101003表示token验证失败
					if (data.code == '101003') {
						mui.alert(data.message + "请重新登录");
						//显示账号登录登陆窗口
						owner.open('accountLogin');
					} else {
						resolve(data);
					}
				},
				error: function(xhr, type, errorThrown) {
					console.log(xhr)
				}
			});
		})
	}
	/**
	 * 功能：getElementById 方法的简写 
	 * 参数: "id" 或 ["id"]
	 * 返回："id" 返回单个 element
	 *      ["id"] 返回数组 [element] 
	 **/
	owner.getId = function() {
		var elements = new Array();
		for (var i = 0; i < arguments.length; i++) {
			var element = arguments[i];
			if (typeof element == 'string')
				element = document.getElementById(element);
			if (arguments.length == 1)
				return element;
			elements.push(element);
		}
		return elements;
	}
	
	owner.getgg = function(membershipCardGid){
		mui.ajax(app.getUrl() +'/selBaseStrategyNameByMembershipCardGid',{
			data:{
				'membershipCardGid': membershipCardGid
			},
			dataType:'json',//服务器返回json格式数据
			type:'get',//HTTP请求类型
			timeout:10000,//超时时间设置为10秒；
			success:function(data){
				gg = data[0].baseStrategyName;
			}
		});
		
	}
}(window.app = {}));
